///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.utils {
    import mx.utils.ObjectUtil;
    import mx.collections.ArrayCollection;
    
    /**
	 * Creates an array of ramdomly organized items.
	 */
    public class RandomOrder {
        public function RandomOrder():void{};
        
        public static function generateRandomArray(numElements:int,exclude:Array=null):Array {
            var origArray:Array = [];
            var resultArray:Array = [];
            
            for (var i:int=0;i<numElements;i++) {
            	if (exclude!=null) if (excludeCheck(exclude,i)) continue;
            	
            	origArray.push(i);
            }
            
            while (origArray.length>0 && resultArray.length<numElements) {
                var rdm:int = getRandomNum(origArray.length-1);
                
                resultArray.push(origArray.splice(rdm,1));
            }
            return resultArray;
        }
        
        private static function excludeCheck(exclude:Array,num:int):Boolean {
        	for (var i:int=0;i<exclude.length;i++) {
        		if (num==exclude[i]) return true;
        	}
        	return false;
        }
        
        public static function randomizeArray(ogArray:Array,rtrnLen:int=0):Array {
			var randomArray:Array = [];
			var orderedArray:Array = ogArray.concat();
			if (rtrnLen==0) rtrnLen = ogArray.length;
			
			for (var n:int=0;n<rtrnLen;n++) {
				var tmpNum:Number = getRandomNum(orderedArray.length-1);
				
				randomArray[n] = orderedArray[tmpNum];
				orderedArray.splice(tmpNum, 1);
			}
			
			return randomArray;
		}
        
        public static function getRandomNum(max:Number, min:Number=0):Number {
			//return a number between 0 and max
			return Math.floor(Math.random() * (max - min + 1)) + min;
		}
    }
}

